<template>
  <div>
    <van-button @click="onFollowed()" v-if="!isFollowed" class="follow-btn" type="info" color="#3296fa" round
      size="small" icon="plus" :loading="isFollowLoading">关注</van-button>
    <van-button @click="onFollowed()" v-else class="follow-btn" round size="small" :loading="isFollowLoading">已关注
    </van-button>
  </div>
</template>

<script>
import { deleteUserFollowed, addUserFollowed } from '@/api/user.js'
export default {
  data () {
    return {
      isFollowLoading: false
    }
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, Object],
      required: true
    }
  },
  // 自定义解析事件
  model: {
    prop: 'isFollowed',
    event: 'updataFollowed'
  },
  methods: {
    // 关注
    async onFollowed () {
      this.isFollowLoading = true
      try {
        // 取消关注
        if (this.isFollowed) {
          await deleteUserFollowed(this.autId)
          this.$toast('取关成功')
        } else {
          // 添加关注
          await addUserFollowed(this.autId)
          this.$toast('关注成功')
        }
        // 同步视图
        // this.articles.is_followed = !this.articles.is_followed
        this.$emit('updataFollowed', !this.isFollowed)
      } catch (err) {
        console.log(err)
        if (err.response && err.response.status === 400) {
          this.$toast('不能关注自己')
        }
        this.$toast('操作失败')
      }
      this.isFollowLoading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
